package com.bite.java_chatroom.service;

import com.bite.java_chatroom.mapper.FriendMapper;
import com.bite.java_chatroom.model.MessageResp;
import com.bite.java_chatroom.model.MessageSession;
import com.bite.java_chatroom.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FriendService {
    @Autowired
    private MessageSessionService messageSessionService;
    @Autowired
    private FriendMapper friendMapper;

    public List<UserInfo> getFriendList(Integer id){
        return friendMapper.selectFriendById(id);
    }

    public List<UserInfo> searchFriends(String userName, Integer myselfId) {
        return friendMapper.selectLikeUserInfo("%"+userName+"%",myselfId);
    }

    public void removeAddFriend(Integer fromId, Integer id) {
        friendMapper.removeAddFriend(fromId, id);
    }


    public void addFriend(Integer fromId, Integer id) {
        MessageSession messageSession = new MessageSession();
        //添加会话
        messageSessionService.addMessageSession(messageSession);
        //添加会话与用户关系表
        messageSessionService.addMessageSessionUser(messageSession.getSessionId(),id);
        messageSessionService.addMessageSessionUser(messageSession.getSessionId(),fromId);
        //删除好友申请表
        friendMapper.removeAddFriend2(fromId,id);
        //添加好友关系表
        friendMapper.insertFriend(fromId,id);
        friendMapper.insertFriend(id,fromId);
    }

    public List<MessageResp> selectAddFriends(Integer userId){
        return friendMapper.selectAddFriends(userId);
    }
}
